﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

using eCitizen.DataContracts;
using eCitizen.eAlerts.Constants;
using eCitizen.DataAccess;

namespace eCitizen.eAlerts.Utilities
{
    public class SmtpManager
    {
        public void Broadcast(SftpConfiguration configuration, EmailMessageList messageList)
        {

            SmtpClient emailClient = new SmtpClient();
            ReportLoggingService messageLoggingService = new ReportLoggingService();

            int totalSuccessfulMessageCount = 0;
            int totalFailedMessageCount = 0;
            
            emailClient.EnableSsl = true;
            

            foreach (EmailMessage emailMessage in messageList.EmailMessages)
            {
                try
                {
                    MailMessage mail = new MailMessage();

                    mail.To.Add(new MailAddress(emailMessage.EmailAddress));
                    mail.Subject = emailMessage.MessageContent;
                    mail.Body = emailMessage.MessageContent;

                    emailClient.Send(mail);
                    totalSuccessfulMessageCount++;
                }
                catch (System.Net.Mail.SmtpException)
                {
                    totalFailedMessageCount++;
                }
            }

            messageLoggingService.LogEmailSendStatus(configuration.Service, totalSuccessfulMessageCount, totalFailedMessageCount);
        }


    }
}
